package com.estate.struts.actions;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.estate.business.PriceProjectService;
import com.estate.business.RecordMemberService;
import com.estate.business.UserInfoService;
import com.estate.vo.Vo;
import com.estate.vo.impl.PriceProjectVo;
import com.estate.vo.impl.RecordMemberVo;
import com.estate.vo.impl.UserInfoVo;

public class ModifyProjInPriceAction extends EstateSupport {
	
	private PriceProjectService priceProjectService;
	private UserInfoService userInfoService;
	private RecordMemberService recordMemberService;
	
	private String modifyId;
	private String method;
	private String currentStage;
	private String userStr;
	
	private int id;
	private String number; // 项目编号
	private String name; // 项目名称
	private String delegateCompany; // 委托单位
	private String delegateUser; // 委托单位联系人
	private String delegatePhone; // 委托单位联系人电话
	private String proCatagory; // 工作项目类别
	private Date receiveDate; // 收到日期
	private String contractId; // 合同ID
	private String estimatePrice; // 合同估算价
	private String submitPrice; // 送审价
	private String proRegion; // 所属地区
	private String industryCatagory; // 行业分类
	private int submitUserId; // 填写人
	private String remark; // 备注
	private String proName; // 单项工程名称
	private String proCompany; // 建设单位名称
	private String proContact; // 建设单位联系人
	private String proContactPhone; // 建设单位联系人电话
	private String workCatagory; // 工作项目分类
	private int stage; // 阶段
	private Date authorDate; // 下发日期
	private int submit; // 是否已上报
	private int recordUserId; // 咨询人员ID
	private UserInfoVo contactUser;
	private int contactUserId;
	
	public String execute() throws Exception{
		String page="";
		if("update".equals(method)){
			PriceProjectVo priceProjectVo=this.getModifyProjectInfo();
			this.setCurrentStage(priceProjectVo.getStage()+"");
			priceProjectService.updateVo(priceProjectVo);
		}
		if(userStr== null)
		page= getPage(Integer.parseInt(this.getCurrentStage()));
		else {
			List<Vo> oldList=recordMemberService.findMemberList(1, Integer.parseInt(this.getModifyId()));
			for(int i=0;i<oldList.size();i++){
				recordMemberService.delete(oldList.get(i));
			}
			List<Vo> voList=this.addMemberInfo();
			for(int i=0;i<voList.size();i++){
				((RecordMemberVo)voList.get(i)).setName(((RecordMemberVo)voList.get(i)).getRecordUser().getName());
			}
			if(voList.size()==0){
				page=getPage(Integer.parseInt(this.getCurrentStage()));
			} else{
				this.getSession().setAttribute("memberList", voList);
			page = "add";}
		}
		return page;
	}
	
	public List<Vo> addMemberInfo() {
		List<Vo> memberList=new ArrayList<Vo>();
		if (userStr != null && !"".equals(userStr) && !",".equals(userStr)) {
			int lastInt = (Integer.parseInt(this.getModifyId()));
			String[] delArray = userStr.split(",");
			
			for (int i = 0; i < delArray.length; i++) {
				RecordMemberVo vo = new RecordMemberVo();
				vo.setProCatagory(1);
				vo.setProId(lastInt);
				vo.setRecordUser((UserInfoVo) userInfoService
						.getUserInfo((Integer.parseInt(delArray[i]))));
				memberList.add(vo);
				recordMemberService.addMember(vo);
			}
				
		}
		return memberList;
	}
	
	public String getPage(int currentStage){
		
		String page="";
		
		switch(currentStage){
		case 1:
			page="projRegister";
			break;
		case 2:
		case 3:
			page="selfVerify";
			break;
		case 4:
		case 5:
			page="engineeringReview";
			break;
		case 6:
		case 7:
			page="tecReview";
			break;
		case 8:
		case 9:
			page="leaderReview";
			break;
		case 10:
			page="projReport";
			break;
		case 11:
			page="projUpload";
			break;
		}
		return page;
	}
	
	public PriceProjectVo getModifyProjectInfo() {
		PriceProjectVo priceProjectVo = new PriceProjectVo();

		priceProjectVo.setId(Integer.parseInt(this.getModifyId()));
		priceProjectVo.setAuthorDate(authorDate);
		priceProjectVo.setContractId(contractId);
		priceProjectVo.setDelegateCompany(delegateCompany);
		priceProjectVo.setContactUser((UserInfoVo)userInfoService.getUserInfo(this.getContactUserId()));
		priceProjectVo.setDelegatePhone(delegatePhone);
		priceProjectVo.setDelegateUser(delegateUser);
		priceProjectVo.setEstimatePrice(estimatePrice);
		priceProjectVo.setIndustryCatagory(industryCatagory);
		priceProjectVo.setName(name);
		priceProjectVo.setNumber(number);
		priceProjectVo.setProCatagory("造价咨询");
		priceProjectVo.setProCompany(proCompany);
		priceProjectVo.setProContact(proContact);
		priceProjectVo.setProName(proName);
		priceProjectVo.setProContactPhone(proContactPhone);
		priceProjectVo.setProRegion(proRegion);
		priceProjectVo.setReceiveDate(receiveDate);
		priceProjectVo.setRecordUserId(recordUserId);
		priceProjectVo.setRemark(remark);
		priceProjectVo.setSubmit(submit);
		priceProjectVo.setStage(priceProjectService.getStageById(Integer.parseInt(this.getModifyId())));
		priceProjectVo.setSubmitPrice(submitPrice);
		// priceProjectVo.setSubmitUserId(priceProjectVo.getRecordUserId());
		priceProjectVo.setWorkCatagory(workCatagory);
		return priceProjectVo;
	}

	public void setModifyId(String modifyId) {
		this.modifyId = modifyId;
	}

	public String getModifyId() {
		return modifyId;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getMethod() {
		return method;
	}

	public void setPriceProjectService(PriceProjectService priceProjectService) {
		this.priceProjectService = priceProjectService;
	}

	public PriceProjectService getPriceProjectService() {
		return priceProjectService;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}
	
	public void setNumber(String number) {
		this.number = number;
	}

	public String getNumber() {
		return number;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setDelegateCompany(String delegateCompany) {
		this.delegateCompany = delegateCompany;
	}

	public String getDelegateCompany() {
		return delegateCompany;
	}

	public void setDelegateUser(String delegateUser) {
		this.delegateUser = delegateUser;
	}

	public String getDelegateUser() {
		return delegateUser;
	}

	public void setDelegatePhone(String delegatePhone) {
		this.delegatePhone = delegatePhone;
	}

	public String getDelegatePhone() {
		return delegatePhone;
	}

	public void setProCatagory(String proCatagory) {
		this.proCatagory = proCatagory;
	}

	public String getProCatagory() {
		return proCatagory;
	}

	public void setReceiveDate(Date receiveDate) {
		this.receiveDate = receiveDate;
	}

	public Date getReceiveDate() {
		return receiveDate;
	}

	public String getContractId() {
		return contractId;
	}

	public void setContractId(String contractId) {
		this.contractId = contractId;
	}

	public void setEstimatePrice(String estimatePrice) {
		this.estimatePrice = estimatePrice;
	}

	public String getEstimatePrice() {
		return estimatePrice;
	}

	public void setSubmitPrice(String submitPrice) {
		this.submitPrice = submitPrice;
	}

	public String getSubmitPrice() {
		return submitPrice;
	}

	public void setProRegion(String proRegion) {
		this.proRegion = proRegion;
	}

	public String getProRegion() {
		return proRegion;
	}

	public void setIndustryCatagory(String industryCatagory) {
		this.industryCatagory = industryCatagory;
	}

	public String getIndustryCatagory() {
		return industryCatagory;
	}

	public void setSubmitUserId(int submitUserId) {
		this.submitUserId = submitUserId;
	}

	public int getSubmitUserId() {
		return submitUserId;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getRemark() {
		return remark;
	}

	public void setProName(String proName) {
		this.proName = proName;
	}

	public String getProName() {
		return proName;
	}

	public void setProCompany(String proCompany) {
		this.proCompany = proCompany;
	}

	public String getProCompany() {
		return proCompany;
	}

	public void setProContact(String proContact) {
		this.proContact = proContact;
	}

	public String getProContact() {
		return proContact;
	}

	public void setProContactPhone(String proContactPhone) {
		this.proContactPhone = proContactPhone;
	}

	public String getProContactPhone() {
		return proContactPhone;
	}

	public void setWorkCatagory(String workCatagory) {
		this.workCatagory = workCatagory;
	}

	public String getWorkCatagory() {
		return workCatagory;
	}

	public void setStage(int stage) {
		this.stage = stage;
	}

	public int getStage() {
		return stage;
	}

	public void setAuthorDate(Date authorDate) {
		this.authorDate = authorDate;
	}

	public Date getAuthorDate() {
		return authorDate;
	}

	public void setSubmit(int submit) {
		this.submit = submit;
	}

	public int getSubmit() {
		return submit;
	}

	public void setRecordUserId(int recordUserId) {
		this.recordUserId = recordUserId;
	}

	public int getRecordUserId() {
		return recordUserId;
	}

	public void setContactUser(UserInfoVo contactUser) {
		this.contactUser = contactUser;
	}

	public UserInfoVo getContactUser() {
		return contactUser;
	}

	public void setContactUserId(int contactUserId) {
		this.contactUserId = contactUserId;
	}

	public int getContactUserId() {
		return contactUserId;
	}
	
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setCurrentStage(String currentStage) {
		this.currentStage = currentStage;
	}

	public String getCurrentStage() {
		return currentStage;
	}

	public void setUserStr(String userStr) {
		this.userStr = userStr;
	}

	public String getUserStr() {
		return userStr;
	}

	public void setRecordMemberService(RecordMemberService recordMemberService) {
		this.recordMemberService = recordMemberService;
	}

	public RecordMemberService getRecordMemberService() {
		return recordMemberService;
	}

}
